package com.cswnode.demo.service.impl;

import com.cswnode.demo.config.security.JwtTokenUtil;
import com.cswnode.demo.dao.UserDao;
import com.cswnode.demo.entity.User;
import com.cswnode.demo.enums.YesOrNoEnum;
import com.cswnode.demo.service.LoginService;
import com.cswnode.demo.util.Constant;
import com.cswnode.demo.vo.loginVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author csw
 * @create 2021/1/26 11:37
 */
@Service("LoginService")
public class LoginServiceImpl implements LoginService , Constant {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户登陆
     * @param user
     * @return
     */
    @Override
    public Map<String, String> login(loginVo user, HttpServletRequest request) {
        Map<String, String> msg = new HashMap<>();
        UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());
        if (null == userDetails){
            msg.put("errorMsg","用户不能为空！");
            return msg;
        }
        if (StringUtils.isBlank(userDetails.getUsername())){
            msg.put("errorMsg","用户名不能为空");
            return msg;
        }
        if (StringUtils.isBlank(user.getPassword())){
            msg.put("errorMsg","密码不能为空");
            return msg;
        }
        User queryByName = userDao.queryByName(user.getUsername());
        if (ObjectUtils.isEmpty(queryByName)){
            msg.put("errorMsg","该用户不存在！");
            return msg;
        }
        if (!(StringUtils.equals(user.getType(),queryByName.getType()))){
            msg.put("errorMsg","用户角色错误，请重新输入！");
            return msg;
        }
        if (YesOrNoEnum.NO.getCode().equals(queryByName.getIsActive())){
            msg.put("errorMsg","用户未激活,请激活");
            return msg;
        }
        if (!(passwordEncoder.matches(user.getPassword(),userDetails.getPassword()))){
            msg.put("errorMsg","密码输入错误，请重新输入！");
            return msg;
        }

        //更新security登录用户对象
        UsernamePasswordAuthenticationToken authenticationToken = new
                UsernamePasswordAuthenticationToken(userDetails, null
                , userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);

        //生成token
        String token = jwtTokenUtil.generateToken(userDetails);
        msg.put("token",token);
        msg.put("tokenHead",tokenHead);
        return msg;
    }


}
